<template>
 <div class='sub-category' v-if="subCate">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区域 -->
      <SubFilter ></SubFilter>
  <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort />
        <!-- 商品列表 -->
        <ul class="list">
          <li v-for="item in list" :key="item.id">
            <GoodsItem :goods="item"></GoodsItem>
          </li>
        </ul>
        <!-- 无序列表 -->
        <XtxInfiniteLoading :loading="loading" :finished='finished' @load="onLoad"></XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from './conponents/sub-bread.vue'
import SubFilter from './conponents/sub-filter.vue'
import SubSort from './conponents/sub-sort.vue'
import GoodsItem from './conponents/goods-item.vue'
import XtxInfiniteLoading from '@/components/library/xtx-infinite-loading.vue'

import { ref, watch, reactive, provide } from 'vue'
import { findCategoryGoods, findSubCategoryFilter } from '@/api/category'
import { useRoute } from 'vue-router'
export default {
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem,
    XtxInfiniteLoading
  },
  name: 'SubCategory',
  setup () {
    const route = useRoute()
    // 加载状态
    const loading = ref(false)
    // 结束状态
    const finished = ref(false)

    let data = {
      page: 1, // 页码
      pageSize: 20 // 页容量
    }
    // 定义响应式数据
    const defaultParams = () => {
      return {
        categoryId: route.params.id, // 分类id
        inventory: false, // 是否库存
        onlyDiscount: false, // 是否显示特惠
        sortField: null, // 排序字段
        sortMethod: undefined, // 价格排序
        attrs: [], // 筛选条件数组 [{groupName:***,propertyName:***}]
        brandId: null // 品牌的id
      }
    }
    // 二级类名
    const subCate = ref(null)
    // 请求参数
    const sortParams = reactive(defaultParams())

    // 导出
    provide('sortParams', sortParams)

    // 商品列表
    const list = ref([])
    // 加载数据
    const onLoad = async () => {
      // 请求前 显示请求动画
      loading.value = true
      // const { result } = await findSubCategoryGoods(data)
      const { result } = await findCategoryGoods(data)

      // 请求完成 - 隐藏加载动画
      loading.value = false
      // console.log(result)
      list.value.push(...result.items)
      //  判断是否没有更多的数据
      if (result.items.length < data.pageSize) {
        // 显示没有更多数据了
        finished.value = true
        return
      }
      data.page++
    }
    // 路由id改变就重新初始化 ，在调用一次
    watch(() => route.params.id, async (id) => {
      if (route.path !== `/category/sub/${id}`) return
      const { result } = await findSubCategoryFilter(id)
      // console.log(result)
      subCate.value = result
      // console.log(subCate)
      // 只要id改变  就让参数复原
      const obj = defaultParams()
      for (const key in obj) {
        sortParams[key] = obj[key]
      }
    }, { immediate: true })
    // 导出二级类目
    provide('subCate', subCate)

    // 监听字段名的变化
    watch(sortParams, () => {
      list.value = []
      finished.value = false
      data = { ...data, ...sortParams, page: 1 }
      onLoad()
    }, { immediate: true })
    return { loading, finished, onLoad, list, sortParams, subCate }
  }
}
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
